import java.io.*;

public class StartMenu extends GameStage {
  boolean ChangeStage = false;
  boolean ChooseName = false;
  PImage img;
  Button newgame;
  Button continuegame;
  Scanner sc;
  ArrayList<Button> menu;
  boolean selectCharacter = false;
  TextBox charname;
  Player p1;

  public StartMenu() {
    img = loadImage("titlescreen.png");
    background(img);
    newgame = new Button(243, 392, 215, 86);
    continuegame = new Button(515, 392, 215, 86);
    menu = new ArrayList<Button>();
  }

  public void keyboardAct() {
    if (ChooseName) {
      if (keyCode == ENTER && !charname.input.equals("")) {
        p1 = new Player(charname.input);
        ChangeStage = true;
        return;
      }
      if (keyCode == BACKSPACE && !charname.input.equals("")) {
        charname.deleteChar();
        fill(255);
        rect(250, 275, 250, 25);
        fill(0);
        text(charname.input, 260, 280, 250, 25);
        return;
      }
      else {
        if (!(key == CODED)) {
          charname.changeText(key);
          if (charname.input != null) {
            fill(255);
            rect(250, 275, 250, 25);
            fill(0);
            text(charname.input, 260, 280, 250, 25);
          }
          return;
        }
      }
    }
  }

  public void mouseAct(int x, int y) {
    if (selectCharacter == true) {
      for (Button b : menu) {
        if (b.isClicked(x, y)) {
          p1 = new Player(b.menuItem);
          ChangeStage = true;
          return;
        }
      }
    }
    if (newgame.isClicked(x, y)) {
      if (!ChooseName && !selectCharacter) {
        ChooseName = true;
        rect(250, 250, 250, 50);
        rect(250, 250, 250, 25);
        fill(0);
        text("Type Character Name", 315, 265);   
        charname = new TextBox(250, 275, 250, 25);
        return;
      }
      if (selectCharacter) {
        img = loadImage("titlescreen.png");
        background(img);
        ChooseName = true;
        fill(255);
        rect(250, 250, 250, 50);
        rect(250, 250, 250, 25);
        fill(0);
        text("Type Character Name", 315, 265);   
        charname = new TextBox(250, 275, 250, 25);
        return;
      }
    }
    if (continuegame.isClicked(x, y)) {
      if (!ChooseName && !selectCharacter) {
        int hor = 60;
        int ver = 50;
        int i = 0;
        sc = new Scanner(createReader("CList.txt"));
        sc.useDelimiter(",");
        while (sc.hasNext ()) {
          String s = sc.next();
          img = loadImage("buttonpic.png");
          image(img, hor, ver);
          menu.add(new Button(hor, ver, 100, 50, s));
          fill(0);
          text(s, hor+50-((s.length()/2)*6), ver+25);
          ver = ver + 100;
          i++;
          selectCharacter = true;
        }
        return;
      }
      if (ChooseName) {
        img = loadImage("titlescreen.png");
        background(img);
        int hor = 60;
        int ver = 50;
        int i = 0;
        sc = new Scanner(createReader("CList.txt"));
        sc.useDelimiter(",");
        while (sc.hasNext ()) {
          String s = sc.next();
          img = loadImage("buttonpic.png");
          image(img, hor, ver);
          menu.add(new Button(hor, ver, 100, 50, s));
          fill(0);
          text(s, hor+50-((s.length()/2)*6), ver+25);
          ver = ver + 100;
          i++;
          selectCharacter = true;
        }
      }
    }
  }
}

